package com.example.demo2.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo2.common.Result;
import com.example.demo2.entity.AskKefu;
import com.example.demo2.entity.Customer;
import com.example.demo2.entity.DTO.AskKefuDTO;
import com.example.demo2.entity.DTO.CustomerDTO;
import com.example.demo2.entity.Question;
import com.example.demo2.service.AskKefuService;
import com.example.demo2.service.CustomerService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/askKefu")
public class AskKefuController {
    @Autowired
    private AskKefuService askKefuService;
    @Autowired
    private CustomerService customerService;

    @PostMapping("/add")
    public Result save(@RequestBody AskKefu askKefu) {
        askKefu.setCreateTime(DateUtil.toLocalDateTime(new Date()));
        boolean save = askKefuService.save(askKefu);
        if (save) {
            return Result.success(save);
        } else {
            return Result.error("-1", save);
        }
    }
    @PostMapping("/all")
    public Result SelectALl() {
        List<AskKefu> list = askKefuService.list();
        if (list != null) {
            return Result.success(list);
        } else {
            return Result.error("-1", list);
        }
    }
    // 删除
    @PostMapping("/delete")
    public Result deleteCustomer(@RequestBody AskKefu askKefu) {
        return askKefuService.removeByIdOfCustomer(askKefu.getId());
    }
    //批量删除
    @PostMapping("/batch/deleteOrder")
    public Result batchDeleteCity(@RequestBody List<Integer> ids) {
        return Result.success(askKefuService.removeBatchByIds(ids));
    }

    //分页查询
    @PostMapping("/findPage")
    public Result findPage(@RequestBody AskKefuDTO askKefuDTO) {
        return Result.success(askKefuService.findPageOfCustomer(askKefuDTO));
    }
    @PostMapping("/SelectById")
    public Result SelectByUserId(@RequestBody Integer id) {

        AskKefu askKefu = new AskKefu();
        askKefu.setCreateTime(DateUtil.toLocalDateTime(new Date()));
        askKefu.setContent("你好，请问有什么能帮助到您的？");
        askKefu.setIsKefu(1);
        HashMap<String, Object> map = new HashMap<>();
        map.put("kefu",askKefu);

        LambdaQueryWrapper<AskKefu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AskKefu::getUserId,id);
        lqw.orderByAsc(AskKefu::getCreateTime);
        List<AskKefu> list = askKefuService.list(lqw);
        if (list != null) {
            Customer oneCustomer = customerService.getById(id);
            map.put("userInfo",oneCustomer);
            map.put("customer",list);
        } else {
            map.put("customer",null);
        }
        return Result.success(map);
    }
}
